package com.coocaa.launcher.settings.deviceinfo;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;

import com.coocaa.launcher.framework.manager.setting.CPUInfos.CPUInfo;
import com.coocaa.launcher.framework.manager.setting.DeviceManager;
import com.coocaa.x.framework.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class DeviceSetting {
    public static final String AA = "_aa_";
    private OnGetDeviceInfoListener mDeviceInfoListener = null;
    private GLSurfaceView mGLSurfaceView;
    private String GPU = "";
    private String GPUVENDOR = "";
    private String GPUVERSION = "";

    public interface OnGetDeviceInfoListener {
        public void onGetDeviceInfo(List<String> deviceInfo);
    }

    public DeviceSetting() {
    }

    public void setGetDeviceInfoListener(OnGetDeviceInfoListener listener) {
        this.mDeviceInfoListener = listener;
    }

    private class Renderer implements GLSurfaceView.Renderer {

        public void onDrawFrame(GL10 gl) {

        }

        public void onSurfaceChanged(GL10 gl, int width, int height) {

        }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            // 渲染器
            GPU = gl.glGetString(GL10.GL_RENDERER);
            LogUtils.d("info", "GL_RENDERER:::::" + GPU);
            // 供应商
            GPUVENDOR = gl.glGetString(GL10.GL_VENDOR);
            LogUtils.d("info", "GL_VENDOR::::: " + GPUVENDOR);
            // 版本
            GPUVERSION = gl.glGetString(GL10.GL_VERSION);
            LogUtils.d("info", "GL_VERSION::::: " + GPUVERSION);
            getDeviceInfo();       //重新刷一下
        }

    }

    public GLSurfaceView getGPU(Context c) {
        mGLSurfaceView = new GLSurfaceView(c);
        mGLSurfaceView.setRenderer(new Renderer());
        mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
        //mGLSurfaceView.setZOrderOnTop(true);
        return mGLSurfaceView;
    }

    public synchronized void getDeviceInfo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<String> list = new ArrayList<String>();
                String cpu = DeviceManager.getInstance().getCPU();
                int cpuCount = DeviceManager.getInstance().getCPUCount();
                CPUInfo info = DeviceManager.getInstance().getCPUInfo();
                float mCPUMaxFreq = (float) info.mCPUMaxFreq / (1000 * 1000);
                String chipName = info.Hardware;
                float memory = DeviceManager.getInstance().getTotalSize();
                float avialMemory = DeviceManager.getInstance().getAvialSize();
                long totalMemory = DeviceManager.getInstance().getTotalMemory();
                String pxi = DeviceManager.getInstance().getPix();
                String mac = DeviceManager.getInstance().getMac();
                String ip = DeviceManager.getInstance().getIpInfo();
                String sdk = DeviceManager.getInstance().getSDK();
                String module = DeviceManager.getInstance().getModule();
                String manuFactory = DeviceManager.getInstance().getManufactory();
                list.add("厂  商" + AA + manuFactory);
                list.add("设  备" + AA + module);
                if (chipName != null && !chipName.equals("")) {
                    cpu += "  " + chipName;
                }
                if (cpuCount > 0) {
                    cpu += "  " + cpuCount + " 核";
                }
                if (mCPUMaxFreq > 0) {
                    cpu += "  " + mCPUMaxFreq + "GHZ";
                }
                list.add("处理器" + AA + cpu);
                if (!GPU.equals(""))
                    list.add("GPU渲染器" + AA + GPU);
                if (!GPUVENDOR.equals(""))
                    list.add("GPU厂商" + AA + GPUVENDOR);
                if (!GPUVERSION.equals(""))
                    list.add("GPU版本" + AA + GPUVERSION);
                list.add("存储空间" + AA + avialMemory + "GB(可用)/" + memory + "GB(总共)");
                list.add("内  存" + AA + totalMemory + "MB");
                list.add("分辨率" + AA + pxi);
                if (mac.equals("00:00:00:00:00:00"))
                    list.add("MAC地址" + AA + "MAC获取失败");
                else
                    list.add("MAC地址" + AA + mac);
                list.add("IP 地址" + AA + ip);
                list.add("Android版本" + AA + sdk);
                if (mDeviceInfoListener != null) {
                    mDeviceInfoListener.onGetDeviceInfo(list);
                }
            }
        }).start();
    }

}
